PROGRAM MAIN
  USE GLOBVAR
  USE INIT
  USE DATAMM 
  USE smm
  USE smm2
  USE NRUTIL
  USE MATRIX
  USE PROBABILITY
  USE ars 
  USE ANNEALING
  USE RANDOM
  USE MINIMIZATION,ONLY: BFGS
  USE SIMPLEX
  USE STATISTICS
  !USE MPI
  
  IMPLICIT NONE
  REAL(8) :: theta(N_var),bound(N_var,2) !, theta_true(N_var)
  REAL(8) :: mu,sig,aa,mm(n_mom),aa2
  LOGICAL :: check
  
  INTEGER :: j,n,ierr,hz,tin,tout,k,i,jj,kk
  myid=0
  CALL Set_Seed(111,727,999999,648208379) 
  CALL INITIALIZE()
  CALL datam(mm)
  moment=mm
   OPEN(6424,file="data.out")
    DO i = 1,N_mom 
      WRITE(6424,'(200F16.8)') moment(i)
    END DO
   CLOSE(6424)
   !stop
  theta=0.1d0
  !WRITE(*,*) N_VAR

                OPEN(1,file="point.out")
                    DO j=1,SIZE(theta)
                       READ(1,fmt=*) theta(j)
                    END DO
                CLOSE(1)
       !IF (myid==0) write(*,*) theta_true
        aa=mom(theta)
        aa2=mom2(theta)
        !theta=theta_true!*0.90d0
       IF (myid==0) write(*,*) aa,aa2
       !stop
      DO i=1,N_var 
       bound(i,1)=min(theta(i)*0.1, theta(i)-2.0d0)
       bound(i,2)=max(theta(i)*2.8, theta(i)+2.0d0)
      END DO
               DO j=1,1
                IF (INT(J/11.0D0)==0) THEN
                          !CALL bfgs(theta,1.0d-6,1.0d-6,mom,0,.False.) 
                          CALL Nelder_Meade(theta,1.0d-8,mom,0,15000)
                          !CALL Nelder_Meade(theta,1.0d-8,mom,0,15000)
                          CALL Nelder_Meade(theta,1.0d-8,mom,0,15000)
                          !CALL Simulated_Annealing(50.0d0,0.80d0,30,theta,mom,150,0,(/1234,6284,123656+j,231365488/))
                          !CALL Accelerated_Random_Search(theta,0.5d0,bound,50,100,mom,.TRUE.,0,(/1,2,3,4+j/)) 
                          CALL Nelder_Meade(theta,1.0d-8,mom,0,15000)
              
                        DO i=1,N_var 
                           bound(i,1)=max(theta(i)*0.1, theta(i)-1.0d0)
                           bound(i,2)=min(theta(i)*1.8, theta(i)+1.0d0)
                        END DO
              
                          !CALL Accelerated_Random_Search(theta,0.5d0,bound,50,100,mom,.TRUE.,0,(/1,2+j,3,4/)) 
                          !CALL Nelder_Meade(theta,1.0d-7,mom,0,5000)
                          !CALL Simulated_Annealing(50.0d0,0.50d0,30,theta,mom,150,0,(/1234,6284+j,123656,231365488/))
                          !CALL Nelder_Meade(theta,1.0d-7,mom,0,5000)
               ELSE
                          CALL Nelder_Meade(theta,1.0d-8,mom2,0,15000)
                          CALL Nelder_Meade(theta,1.0d-8,mom2,0,15000)
                          CALL Nelder_Meade(theta,1.0d-8,mom2,0,15000)  
                          !CALL bfgs(theta,1.0d-4,1.0d-5,mom2,0,.False.)                      
                          CALL Simulated_Annealing(50.0d0,0.80d0,30,theta,mom2,150,0,(/1234+j,6284,123656,231365488/))                  
                          !CALL Accelerated_Random_Search(theta,0.5d0,bound,50,100,mom2,.TRUE.,0,(/1+j,2,3,4/)) 
                          CALL Nelder_Meade(theta,1.0d-8,mom2,0,15000)
                        DO i=1,N_var 
                           bound(i,1)=max(theta(i)*0.1, theta(i)-1.0d0)
                           bound(i,2)=min(theta(i)*1.8, theta(i)+1.0d0)
                        END DO
                       !CALL Accelerated_Random_Search(theta,0.5d0,bound,50,100,mom2,.TRUE.,0,(/1,2,3,4+j/)) 
                       CALL Nelder_Meade(theta,1.0d-7,mom2,0,15000)
                       CALL Simulated_Annealing(50.0d0,0.50d0,30,theta,mom2,150,0,(/1234,6284,123656+j,231365488/))
                       CALL Nelder_Meade(theta,1.0d-7,mom2,0,15000)
                  END IF
              END DO

  
END PROGRAM MAIN
